package grsu.epam.airline.service;

import grsu.epam.airline.DAO.IAirportDAO;
import grsu.epam.airline.DAOImpl.DAOFactory;
import grsu.epam.airline.constant.Constant;
import grsu.epam.airline.model.Airport;

import java.sql.SQLException;
import java.util.ArrayList;

public class AirportService {

	public void addAirport(String airportName) {
		Airport airport = new Airport(airportName);
		IAirportDAO airportDao = (IAirportDAO) DAOFactory.getInstance().getDAO(
				Constant.AirportDAO);
		try {
			airportDao.addAirport(airport);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance()
					.closeConnection(airportDao.getConnection());
		}
	}

	public ArrayList<Airport> getAllAirport() {
		IAirportDAO airportDao = (IAirportDAO) DAOFactory.getInstance().getDAO(
				Constant.AirportDAO);
		ArrayList<Airport> airports = null;
		try {
			airports = (ArrayList<Airport>) airportDao.getAllAirport();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance()
					.closeConnection(airportDao.getConnection());
		}
		return airports;
	}

	public void deleteAirport(int airportId) {
		IAirportDAO airportDao = (IAirportDAO) DAOFactory.getInstance().getDAO(
				Constant.AirportDAO);
		try {
			airportDao.deleteAirport(airportId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance()
					.closeConnection(airportDao.getConnection());
		}
	}

	public Airport getAirportById(int airportId) {
		IAirportDAO airportDao = (IAirportDAO) DAOFactory.getInstance().getDAO(
				Constant.AirportDAO);
		Airport airport = null;
		try {
			airport = (Airport) airportDao.getAirportById(airportId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance()
					.closeConnection(airportDao.getConnection());
		}
		return airport;
	}

	public void updateAirport(int airportId, String airportName) {
		IAirportDAO airportDao = (IAirportDAO) DAOFactory.getInstance().getDAO(
				Constant.AirportDAO);
		try {
			airportDao.updateAirport(new Airport(airportId, airportName));
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance()
					.closeConnection(airportDao.getConnection());
		}
	}
}
